package com.letoken.platform.admin.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.pub.res.ImAppRes;
import com.letoken.platform.repo.app.res.user.ConferenceConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/conference/config")
public class ConferenceConfigController {

    @Value("${im.app.url}")
    private String imAppUrl;

    @Permission(uri = "/conference/config/get" ,qx = "/conference/config/get")
    @GetMapping("/get")
    public ApiResponse get() {

        int nonce = (int)(Math.random() * 100000 + 3);
        long timestamp = System.currentTimeMillis();
        String str = nonce + "|/conference/config/admin|" + timestamp;
        String sign = DigestUtils.sha1Hex(str);

        Map<String, String> headers = new HashMap<>();
        headers.put("sign",sign);
        headers.put("nonce", String.valueOf(nonce));
        headers.put("timestamp", String.valueOf(timestamp));
        try(HttpResponse httpResponse = HttpUtil.createGet(imAppUrl + "/conference/config/get/admin").addHeaders(headers).execute()){
            if(httpResponse.isOk()){
                try {
                    ImAppRes imAppRes =   JSON.parseObject(httpResponse.bodyBytes(), ImAppRes.class);
                    return ApiResponse.create(imAppRes.code, imAppRes.message, imAppRes.result);
                }catch (Exception e){
                    return ApiResponse.create(90000003, "解析数据异常");
                }
            }
            return ApiResponse.create(90000002,"网络请求失败");
        }catch (Exception e){
            return ApiResponse.create(90000002,"网络异常");
        }
    }


    @Permission(uri = "/conference/config/update" ,qx = "/conference/config/update")
    @PostMapping("/update")
    public ApiResponse update(@RequestBody ConferenceConfig conferenceConfig) {

        int nonce = (int)(Math.random() * 100000 + 3);
        long timestamp = System.currentTimeMillis();
        String str = nonce + "|/conference/config/admin|" + timestamp;
        String sign = DigestUtils.sha1Hex(str);

        Map<String, String> headers = new HashMap<>();
        headers.put("sign",sign);
        headers.put("nonce", String.valueOf(nonce));
        headers.put("timestamp", String.valueOf(timestamp));
        try(HttpResponse httpResponse = HttpUtil.createPost(imAppUrl + "/conference/config/update/admin").body(JSON.toJSONString(conferenceConfig)).addHeaders(headers).execute()){
            if(httpResponse.isOk()){
                try {
                    ImAppRes imAppRes = JSON.parseObject(httpResponse.bodyBytes(), ImAppRes.class);
                    return ApiResponse.create(imAppRes.code, imAppRes.message, imAppRes.result);
                }catch (Exception e){
                    return ApiResponse.create(90000003, "解析数据异常");
                }
            }
            return ApiResponse.create(90000002,"网络请求失败");
        }catch (Exception e){
            return ApiResponse.create(90000002,"网络异常");
        }
    }



}
